import axios from 'axios'
import stroe from '../../store/index'
import router from '@/router'
import { Indicator } from 'mint-ui';
export default {
 
  common: {
    method: 'GET',
    params: {},
    data: {},
    headers:{}
  },
  
  $axios(options = {}) {
    options.method = options.method || this.common.method
    //get
    options.params = options.params || this.common.params
    //post
    options.data = options.data || this.common.data

    options.headers = options.headers || this.common.headers
   
    Indicator.open('加载中...')

    //判断是否传入headers:{token:true},判断用户是否登录
    if (options.headers.token) {
      options.headers.token = stroe.state.user.token
      if (!options.headers.token) {
        router.push('/login')
      }
    }


    return axios(options).then(v => {
      let data = v.data.data
      return new Promise((res, rej) => {
        if (!v) {
          return rej()
        }
        setTimeout(() => {
          Indicator.close();
        },300)
        res(data)
      })
    })
  }
}